package com.eastseeway.radioproject.engineer;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.eastseeway.radioproject.R;
import com.eastseeway.radioproject.databinding.ItemEngineerRvBinding;

public class EngineerItemAdapter extends ListAdapter<EngineerItemBean, EngineerItemAdapter.ItemViewHolder> {

    public EngineerItemAdapter() {
        super(new DiffUtil.ItemCallback<EngineerItemBean>() {
            @Override
            public boolean areItemsTheSame(EngineerItemBean oldItem, EngineerItemBean newItem) {
                return oldItem.getItemTitle().equals(newItem.getItemTitle());
            }

            @Override
            public boolean areContentsTheSame(EngineerItemBean oldItem, EngineerItemBean newItem) {
                return oldItem.getItemValue().equals(newItem.getItemValue());
            }
        });
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemEngineerRvBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_engineer_rv, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.getBinding().setItemBean(getItem(position));
    }


    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        private ItemEngineerRvBinding binding;

        public ItemViewHolder(ItemEngineerRvBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void setBinding(ItemEngineerRvBinding binding) {
            this.binding = binding;
        }

        public ItemEngineerRvBinding getBinding() {
            return this.binding;
        }
    }

}
